"""死锁"""
from concurrent.futures import ThreadPoolExecutor
from threading import Lock


def thread_run_1(num):
    global global_num
    for i in range(num):
        lock_1.acquire()  # lock_1 上锁
        global_num += 1
        print("thread_run_1 is locked")
        lock_2.acquire()  # 等待lock_2释放，然后获取上锁，如果lock_2没有释放，一直等待
        print("thread_run_1：get the lock_2")
        lock_2.release()  # lock_2 解锁
        lock_1.release()  # lock_1 解锁
    print("In thread 1, the global_num is {}".format(global_num))


def thread_run_2(num):
    global global_num
    for i in range(num):
        lock_2.acquire()  # lock_2 上锁
        global_num += 1
        print("thread_run_2 is locked")
        lock_1.acquire()  # 等待lock_1释放，然后获取上锁，如果lock_1没有释放，一直等待
        print("thread_run_2：get the lock_1")
        lock_1.release()  # lock_1 解锁
        lock_2.release()  # lock_2 解锁
    print("In thread 2, the global_num is {}".format(global_num))


if __name__ == "__main__":
    global_num = 0

    lock_1 = Lock()
    lock_2 = Lock()

    with ThreadPoolExecutor() as executor:
        executor.submit(thread_run_1, 10000)
        executor.submit(thread_run_2, 10000)
    print("Finally, the global_num is {}".format(global_num))

